package com.heima.freemarker;

import com.heima.freemarker.pojo.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DiXiaoNie
 * @since 2022/12/19 16:09
 */
@SpringBootTest
public class GenerHtml {

    @Autowired
    private Configuration configuration;

    @Test
    void gengerHtml() {
        //1.获取模板
        try {
            Template template = configuration.getTemplate("02-list.ftl");

            Map resultMap = dateModel();

            template.process(resultMap,new FileWriter("D:\\develop\\list.html"));

        } catch (Exception e) {
            e.printStackTrace();

        }

        //2.获取数据模型


        //3.生成静态html文件
    }

    public Map dateModel() {
        Map<String, Object> map = new HashMap<>();

        //小强对象模型数据
        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge(18);
        stu1.setMoney(1000);

        //小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200);
        stu2.setAge(19);

        //将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);

        //向map中存放List集合数据
        map.put("stus", stus);


        //创建Map数据
        HashMap<String, Student> stuMap = new HashMap<>();
        stuMap.put("stu1", stu1);
        stuMap.put("stu2", stu2);
        //向map中存放Map数据
        map.put("stuMap", stuMap);

        //返回Map
        return map;
    }
}
